package org.jeecg.modules.demo.xcx.training.registration.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.xcx.training.registration.entity.Registration;
import org.jeecg.modules.demo.xcx.training.registration.service.IRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 培训报名
 */
@Api(tags="registration")
@RestController
@RequestMapping("/training/registration")
@Slf4j
public class RegistrationController extends JeecgController<Registration, IRegistrationService> {

    @Autowired
    private IRegistrationService registrationService;


    /**
     * 分页列表查询
     *
     * @param registration
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "registration-分页列表查询")
    @ApiOperation(value="registration-分页列表查询", notes="registration-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<Registration>> queryPageList(Registration registration,
                                                     @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                     HttpServletRequest req) {
        QueryWrapper<Registration> queryWrapper = QueryGenerator.initQueryWrapper(registration, req.getParameterMap());
        Page<Registration> page = new Page<Registration>(pageNo, pageSize);
        IPage<Registration> pageList = registrationService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     *   添加
     *
     * @param registration
     * @return
     */
    @AutoLog(value = "registration-添加")
    @ApiOperation(value="registration-添加", notes="registration-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody Registration registration){
        registrationService.save(registration);
        return Result.OK("添加成功！");
    }


    /**
     *  编辑
     *
     * @param registration
     * @return
     */
    @AutoLog(value = "registration-编辑")
    @ApiOperation(value="registration-编辑", notes="registration-编辑")
    //@RequiresPermissions("tradeorder:tradeorder:edit")
    @PostMapping(value = "/edit")
    public Result<String> edit(@RequestBody Registration registration) {
        registrationService.updateById(registration);
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "registration-通过id删除")
    @ApiOperation(value="registration-通过id删除", notes="registration-通过id删除")
    //@RequiresPermissions("tradeorder:tradeorder:delete")
    @PostMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        registrationService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "registration-批量删除")
    @ApiOperation(value="registration-批量删除", notes="registration-批量删除")
    //@RequiresPermissions("tradeorder:tradeorder:deleteBatch")
    @PostMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.registrationService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

}
